'''Controller class implementation'''

import os,sys
import soya,soya.sdlconst as sdlc
import soya.pudding as pudding
from consts import *


class Action:
    
    def __init__(self,action):
        self.action=action

class Controller:
    
    def __init__(self):
        self.clear()
        print "Keyboard Controller created"
        
    def clear(self):
        self.k_left=self.k_right=self.k_up=self.k_down=0
        pass
    
    def tick(self):
        #self.clear()
        for event in pudding.process_event():
        #for event in soya.process_event():
            
            if event[0]==sdlc.KEYDOWN:
                if event[1]==sdlc.K_q: return Action(GAME_QUIT)
                elif event[1]==sdlc.K_UP: self.k_up=1
                elif event[1]==sdlc.K_DOWN: self.k_down=1
                elif event[1]==sdlc.K_LEFT: self.k_left=1
                elif event[1]==sdlc.K_RIGHT: self.k_right=1
                elif event[1]==sdlc.K_PAGEDOWN: return Action(CAM_DOWN)
                elif event[1]==sdlc.K_PAGEUP: return Action(CAM_UP)
                elif event[1]==sdlc.K_INSERT :return Action(CAM_ZOOM_IN)
                elif event[1]==sdlc.K_DELETE:return Action(CAM_ZOOM_OUT)
                elif event[1]==sdlc.K_ESCAPE:return Action(GO_MENU)
                elif event[1]==sdlc.K_RETURN:return Action(MENU_ENTER)
                elif event[1]==sdlc.K_f:return Action(FULLSCREEN)
                elif event[1]==sdlc.K_c:return Action(CHANGE_CAMERA)
                elif event[1]==sdlc.K_v:return Action(TESTING)

            if event[0]==sdlc.KEYUP:
                if event[1]==sdlc.K_UP: self.k_up=0
                elif event[1]==sdlc.K_DOWN: self.k_down=0
                elif event[1]==sdlc.K_LEFT: self.k_left=0
                elif event[1]==sdlc.K_RIGHT: self.k_right=0
            else:
                pass
        if self.k_down==self.k_left==self.k_right==self.k_up==0:pass
        else:
            return Action({
               (1,0,0,0):ACT_FORWARD,
               (0,1,0,0):ACT_BACKWARD,
               (0,0,1,0):ACT_LEFT,
               (0,0,0,1):ACT_RIGHT,
               }.get((self.k_up, self.k_down, self.k_left, self.k_right),ACT_WAIT))

if __name__=="__main__":
    soya.init()
    pudding.init()
    c=Controller()
    while True:
        t=c.tick()
        if t:print t
    